package com.example.demo.controller;


import com.example.demo.config.LoginHandlerInterceptor;
import com.example.demo.config.MyExceptionHandler;
import com.example.demo.pojo.*;
import com.example.demo.service.Impl.EnchantServiceImpl;
import com.example.demo.utils.AliOSSUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class EnchantController {
    @Autowired
    private EnchantServiceImpl enchantService;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    //勾魂
    //勾魂人数总数搜索
    //勾魂计划
    @GetMapping("/getEnchantData")
    public Result queryEnchant(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit,
                               String name){
        PageInfo list = enchantService.queryEnchant(page, limit, name);
        return PageResult.success03(list.getList(),(int) list.getTotal());
    }
    //勾魂完成
    @PutMapping("/handleEnchanter")
    public Result setEnchant(@RequestBody Lifebook lifebook){
        enchantService.setEnchant(lifebook);
        return Result.success("操作成功");
    }
    //勾魂使者列表
    @GetMapping("/getEnchanterList")
    public Result queryEnchanter(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit,
                                    String id, String name){
        if (id == null){
            PageInfo list = enchantService.queryEnchanter(page,limit,name);
            return PageResult.success03(list.getList(), (int) list.getTotal());
        }else {
            return Result.error("搜索失败");
        }
    }
    //勾魂使者添加
    @PostMapping("/addEnchanter")
    public Result addEnchanter(@RequestBody MultipartFile enchant,String name,
                               Integer gender,String description,
                               Integer status) throws IOException {
        String upload = aliOSSUtils.upload(enchant);
        enchantService.addEnchanter(upload,name,gender,description,status);
        return Result.success("添加成功");
    }
    //勾魂使者修改
    @PutMapping("/updateEnchanter")
    public Result updateEnchanter(@RequestBody Enchanter enchanter){
            enchantService.updateEnchanter(enchanter);
            return Result.success("修改成功");
    }
    //生死簿删除
    @DeleteMapping("/deleteEnchanter")
    public Result deleteEnchanter(String  uuid){
        enchantService.deleteEnchanter(uuid);
        return Result.success("删除成功");
    }

}
